package com.vaannila.file;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileManager {
	
	
	
 static public void doDownload( HttpServletRequest req, HttpServletResponse resp, ServletContext sc,
            String filename, String original_filename ) throws IOException
	{
	File                f        = new File(filename); 
	int                 length   = 0;
	ServletOutputStream op       = resp.getOutputStream();
	ServletContext      context  = sc;
	String              mimetype = context.getMimeType( filename );
	
	//
	//  Set the response and go!
	//
	//
	resp.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
	resp.setContentLength( (int)f.length() );
	resp.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
	
	//
	//  Stream to the requester.
	//
	byte[] bbuf = new byte[1000];
	 
	DataInputStream in = new DataInputStream( sc.getResourceAsStream("/resources/css/style.css") );
	
	while ((in != null) && ((length = in.read(bbuf)) != -1))
		{
		op.write(bbuf,0,length);
		}
	
	in.close();
	op.flush();
	op.close();
	}
}
